package algorithms.leecode;

public class Au72 {
    public static void main(String[] args) {
        int res = new Au72().minDistance("intention","execution");
        System.err.println(res);
    }
    public int minDistance(String word1, String word2) {
        int len1 = word1.length() + 1;
        int len2 = word2.length() + 1;
        char[] word1c = word1.toCharArray();
        char[] word2c = word2.toCharArray();
        int dp[][] = new int[len1][len2];
        for (int j = 0; j < len2; j++) {
            for (int i = 0; i < len1 ; i++) {
                if (i == 0) {
                    dp[i][j] = j;
                    continue;
                }
                if (j == 0) {
                    dp[i][j] = i;
                    continue;
                }
                int leftp = dp[i][j-1] + 1;
                int dowmp = dp[i - 1][j] + 1;
                int leftdp = dp[i - 1][j - 1];
                if (word1c[i - 1] != word2c[j - 1] ) {
                    leftdp = leftdp + 1;
                }
                dp[i][j] = Math.min(Math.min(leftdp,dowmp),leftp);

            }
        }
        
        return dp[len1 - 1][len2 -1];
    }
}
